Using a notebook

The purpose of this notebook is to introduce the Jupyter interface. This notebook is a guide to the Jupyter interface and writing code and text in Jupyter notebooks with the Python programming language and Markdown, the lightweight markup language.

This notebook was originally created for a Digital Mixer session at the 2016 STELLA Unconference

Cells

The basic structure of a Jupyter notebook consists of linear sequence of cells from the top to the bottom of the page. A cell's content can consist of either:

1. code and code output


In [2]:
# create a range of numbers
numbers = range(0, 5)
# print out each of the numbers in the range
for number in numbers:
    print(number)


0
1
2
3
4

2. Markdown/html

This is a big header written in markdown

This is a medium header written in markdown

This is a small header written in markdown

This is a paragraph written in markdown

3. raw text

This is ugly. You probably don't need to use raw text.

Click on any text above to see what cell it belongs to. The active cell will be surrounded by a green or blue outline. A green outline indicates you are in edit mode for that cell and you can type in the cell. A blue outline indicates that you are in command mode and you cannot type in the active cell. To enter edit mode in a cell click on any code input area or double-click on any rendered Markdown text.

Notice that you can see the content type of the active cell in the multi-choice button in the notebook toolbar at the top of the page:

You can also use this button to change the cell type.

Adding, removing, and moving cells

You can manage cells using the notebook toolbar.

  • Adding a cell: To add a new cell below the active cell, click
  • Cut/copy/paste a cell: Use to cut or to copy a cell and to paste the cut/copied cell below the active cell
  • Move a cell: To move the active cell up or down, click or
  • Delete a cell: To delete the active cell, click Edit > Delete Cells

Try adding a new Markdown cell and a Code cell, moving them around, and deleting them. If you accidentally delete something you shouldn't have, you can undo it by going to: Edit > Undo Delete Cells

Running a cell

To run code in a cell or to render markdown as html in a cell you must run the cell.

To run the contents of a cell:

  1. activate it
  2. press shift+return or click in the notebook toolbar at the top of the page.

Try running the three Python code cells below. You can edit and re-run a cell as many times as you want.


In [ ]:
# this is Python code -> RUN IT
x = 2

# the output of the last line of code is shown below the cell
x * x

In [ ]:
# this is Python code -> RUN IT
x = 2

# you can also use the 'print' statement to print information to the output below the cell
print(x)

# the output of the last line of code is still shown below the cell
x * x

In [ ]:
# this is Python code -> RUN IT
x = 2

# if there is an error in your code an error message will display in the output below the cell
x + "two"

Try editing and adding some text this Markdown cell

Double-click on this text to start

when you are done press shift+return to run

For an overview of Markdown format check out Markdown Cheatsheet

More information on the UI

  • In the notebook Menubar click Help > User Interface Tour for a quick, guided overview of the user interface.
  • Another overview of the interface from the Jupyter website: Overview of the Notebook UI
  • More information on running code from the Jupyter website: Running Code

Wrap up

You should have a basic understanding of how to navigate an interactive notebook, the cell types, how to manipulate cells (adding, moving, removing, etc.), and how to run cells.

Next: For a basic overview of data analysis and visualization with pandas and matplotlib, download the following notebook and add it to your notebooks folder by uploading it in the Notebook dashboard: http://nbviewer.jupyter.org/github/WaltGurley/jupyter-notebooks-intro/blob/master/Jupyter%20-%20coding%20with%20Python.ipynb